package com.changgou.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhengjd
 * @date 2020/4/29 0:03
 */
@RestController
@RequestMapping("brand")
@CrossOrigin   //用于解决js跨域问题-在微服务中，一般都需要此注解
public class BrandController {

    @Autowired
    private BrandService brandService;


    /***
     * 根据分类实现品牌列表查询
     * /brand/category/{id}  分类ID
     */
    @GetMapping(value = "/category/{id}")
    public Result<List<Brand>> findBrandByCategory(@PathVariable(value = "id")Integer categoryId){
        //调用Service查询品牌数据
        List<Brand> categoryList = brandService.findByCategory(categoryId);
        return new Result<List<Brand>>(true, StatusCode.OK, "查询成功！", categoryList);
    }

    /**
     * 查询所有品牌
     * @return
     */
    //@RequestMapping("findAll")
    @GetMapping
    public Result<List<Brand>> findAll(){
        /*System.out.println("准备休眠，测试并发数量"+Thread.currentThread());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("休眠结束，测试并发数量"+Thread.currentThread());*/
        List<Brand> brandList = brandService.findAll();
        //int i = 1/0;
        return new Result<List<Brand>>(true, StatusCode.OK, "查询所有品牌成功", brandList);
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<Brand> findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK, "查询品牌成功", brand);
    }

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result<>(true, StatusCode.OK, "添加品牌成功");
    }

    /**
     * 修改品牌
     */
    @PutMapping("{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result<>(true, StatusCode.OK, "修改品牌成功");
    }

    /**
     * 指定id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除品牌成功");
    }

    /**
     * 根据条件查询品牌
     * @param brand
     * @return
     */
    @PostMapping(value = "search")
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        List<Brand> brandList = brandService.findList(brand);
        return new Result<List<Brand>>(true, StatusCode.OK, "查询品牌成功", brandList);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    /*@PostMapping(value = "search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size){
        PageInfo<Brand> pageInfo = brandService.findPage(page,size);
        return new Result<>(true, StatusCode.OK, "分页查询品牌成功", pageInfo);
    }*/

    /**
     * 带品牌查询条件的分页查询
     * @param page
     * @param size
     * @param brand
     * @return
     */
    @PostMapping(value = "search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@RequestBody(required = false) Brand brand,@PathVariable Integer page, @PathVariable Integer size){
        PageInfo<Brand> pageInfo = brandService.findPage(page,size,brand);
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "分页查询品牌成功", pageInfo);
    }
}
